﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;

namespace Gype.UI
{
    public class LinearMatrix
    {
        public float M11, M12, M13;
        public float M21, M22, M23;
        public float M31, M32, M33;

        public Vector2 TransformPoint(Vector2 point)
        {
            return new Vector2(M11 * point.X + M12 * point.Y + M13, M21 * point.X + M22 * point.Y + M23);
        }

        public static LinearMatrix operator * (LinearMatrix m1, LinearMatrix m2)
        {
            LinearMatrix ret = new LinearMatrix()
            {
                M11 = m1.M11 * m2.M11 + m1.M12 * m2.M21 + m1.M13 * m2.M31,
                M21 = m1.M21 * m2.M11 + m1.M22 * m2.M21 + m1.M23 * m2.M31,
                M31 = m1.M31 * m2.M11 + m1.M32 * m2.M21 + m1.M33 * m2.M31,

                M12 = m1.M11 * m2.M12 + m1.M12 * m2.M22 + m1.M13 * m2.M32,
                M22 = m1.M21 * m2.M12 + m1.M22 * m2.M22 + m1.M23 * m2.M32,
                M32 = m1.M31 * m2.M12 + m1.M32 * m2.M22 + m1.M33 * m2.M32,

                M13 = m1.M11 * m2.M13 + m1.M12 * m2.M23 + m1.M13 * m2.M33,
                M23 = m1.M21 * m2.M13 + m1.M22 * m2.M23 + m1.M23 * m2.M33,
                M33 = m1.M31 * m2.M13 + m1.M32 * m2.M32 + m1.M33 * m2.M33
            };

            return ret;
        }

        public static LinearMatrix Rotation(float theta)
        {
            LinearMatrix ret = new LinearMatrix()
            {
                M11 = (float)Math.Cos(theta),
                M12 = -(float)Math.Sin(theta),
                M13 = 0,
                M21 = (float)Math.Sin(theta),
                M22 = (float)Math.Cos(theta),
                M23 = 0,
                M31 = 0,
                M32 = 0,
                M33 = 1
            };

            return ret;
        }

        public static LinearMatrix Scale(float X, float Y)
        {
            LinearMatrix ret = new LinearMatrix()
            {
                M11 = X,
                M12 = 0,
                M13 = 0,
                M21 = 0,
                M22 = Y,
                M23 = 0,
                M31 = 0,
                M32 = 0,
                M33 = 1
            };

            return ret;
        }

        public static LinearMatrix Translation(float X, float Y)
        {
            LinearMatrix ret = new LinearMatrix()
            {
                M11 = 1,
                M12 = 0,
                M13 = X,
                M21 = 0,
                M22 = 1,
                M23 = Y,
                M31 = 0,
                M32 = 0,
                M33 = 1
            };

            return ret;
        }

        public static LinearMatrix Identity = new LinearMatrix()
        {
            M11 = 1,
            M12 = 0,
            M13 = 0,
            M21 = 0,
            M22 = 1,
            M23 = 0,
            M31 = 0,
            M32 = 0,
            M33 = 1
        };
    }
}
